package com.project.cheat;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientServers extends JFrame {
  private JTextArea textArea;
  private JTextField input;
  private JButton jb1;
  private JButton jb2;
  private JButton jb3;
  private JButton jb4;
  private JButton jb5;
  public int port = 8888;
  public Socket socket = null;
  private ObjectInputStream in;
  private ObjectOutputStream out;

  // 接受消息
  public void LinktoServer() throws IOException {
    this.in = new ObjectInputStream(socket.getInputStream());

    try {
      while (true) {
        // 阻塞
        Response response = (Response) in.readObject();
        if (response.isIslogin()) {
          textArea.append("服务器说:" + response.getMsg() + "\n");
          jb5.setEnabled(true);
        } else {
          textArea.append("服务器说："+response.getMsg()+"\n");
        }
      }
    } catch (IOException | ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  // 保持连接
  public void Link() {
    try {
      ClientServers.this.socket = new Socket("127.0.0.1", 8888);
      this.out = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
      e.printStackTrace();
    }
    while (true) {
      try {
        LinktoServer();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  // 发送消息
  public void send(Request request) {
    try {
      if (!request.isIslogin()) {
        textArea.append("我说：" + request.getMsg() + "\n");
//        this.out.writeObject(request);
      } else {
        this.out.writeObject(request);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // 断开连接
  //  public void stoplink() throws IOException {
  //    this.jb2.setEnabled(true);
  //    this.jb3.setEnabled(false);
  //    this.textArea.append("已断开连接... \n");
  //    this.socket = null;
  //  }

  // 校验账号密码
  //  public void checkaccount(String username, String pwd) {
  //    for (user u : users) {
  //      if (u.getAccount().equals(username) && u.getPassword().equals(pwd)) {
  //        textArea.append("你好呀：" + username + "\n");
  //        jb3.setEnabled(true);
  //        jb5.setEnabled(true);
  //        jb1.setEnabled(false);
  //      } else {
  //        textArea.append("账号或密码错误，请重试");
  //      }
  //    }
  //  }

  // 创建账号密码
  //  public void createaccount(String username, String pwd) {
  //    this.users.add(new user(username, pwd));
  //    textArea.append("用户创建成功");
  //  }

  public ClientServers() throws HeadlessException {
    //    users.add(new user("root", "123456")); // 创建一个用户
    this.setTitle("❤网络姻缘一线牵❤ (客户端)");
    this.jb1 = new JButton("用户登录");
    this.jb1.setEnabled(false);
    jb1.setBounds(40, 20, 100, 50);
    this.jb2 = new JButton("用户注册");
    this.jb2.setEnabled(false);
    jb2.setBounds(180, 20, 100, 50);
    this.jb3 = new JButton("连接至服务器");
    jb3.setEnabled(true);
    jb3.setBounds(320, 20, 100, 50);
    this.jb4 = new JButton("退出");
    jb4.setBounds(460, 20, 100, 50);
    this.textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setEditable(false);
    textArea.setBounds(50, 80, 500, 300);
    this.input = new JTextField();
    input.setBounds(50, 400, 400, 30);
    this.jb5 = new JButton("发送");
    jb5.setEnabled(false);
    jb5.setBounds(450, 400, 110, 30);
    this.add(jb1);
    this.add(jb2);
    this.add(jb3);
    this.add(jb4);
    this.add(jb5);
    this.add(textArea);
    this.add(input);

    this.setBounds(420, 250, 600, 500);
    this.setLayout(null);
    this.setResizable(false);
    this.setVisible(true);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jb1.addActionListener(
        e -> {
          JFrame jf = new JFrame("Login");
          jf.setLayout(null);
          jf.setBounds(570, 420, 300, 200);
          JLabel text = new JLabel("用户名:");
          JTextField input_user = new JTextField();
          input_user.setBounds(140, 30, 120, 30);
          text.setBounds(50, 30, 50, 30);
          JLabel text2 = new JLabel("密　码:");
          JPasswordField input_pwd = new JPasswordField();
          input_pwd.setBounds(140, 70, 120, 30);
          text2.setBounds(50, 70, 50, 30);
          JButton jb = new JButton("login >>");
          jb.setBounds(200, 110, 70, 35);
          jf.add(text);
          jf.add(input_user);
          jf.add(text2);
          jf.add(input_pwd);
          jf.add(jb);
          jf.setVisible(true);
          jf.setResizable(false);
          jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
          jb.addActionListener(
              e1 -> {
                String user = input_user.getText();
                String pwd = new String(input_pwd.getPassword());
                send(new Request(user, pwd, true));
                jf.dispose();
              });
        });

    jb3.addActionListener(
        e2 -> {
          new Thread(this::Link).start();
          this.textArea.append("已连接至" + port + "... \n");
          this.jb2.setEnabled(true);
          this.jb1.setEnabled(true);
          this.jb3.setEnabled(false);
        });
    jb2.addActionListener(
        e3 -> {
          JFrame jf = new JFrame("注册");
          jf.setLayout(null);
          jf.setBounds(570, 420, 300, 200);
          JLabel text = new JLabel("用户名:");
          JTextField input_user = new JTextField();
          input_user.setBounds(140, 30, 120, 30);
          text.setBounds(50, 30, 50, 30);
          JLabel text2 = new JLabel("密　码:");
          JPasswordField input_pwd = new JPasswordField();
          input_pwd.setBounds(140, 70, 120, 30);
          text2.setBounds(50, 70, 50, 30);
          JButton jb = new JButton("注册 >>");
          jb.setBounds(200, 110, 70, 35);
          jf.add(text);
          jf.add(input_user);
          jf.add(text2);
          jf.add(input_pwd);
          jf.add(jb);
          jf.setVisible(true);
          jf.setResizable(false);
          jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
          jb.addActionListener(
              e1 -> {
                String user = input_user.getText();
                String pwd = new String(input_pwd.getPassword());
                send(new Request(user, pwd, true));
              });
        });
    jb4.addActionListener(e4 -> this.dispose());
    jb5.addActionListener(
        e5 -> {
          String msg = input.getText();
          input.setText(null);
          send(new Request(msg));
        });
  }

  public static void main(String[] args) {
    new ClientServers();
  }
}
